package com.example.front.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.utils.MyAdapter;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 桌游界面类
 */
public class SwitchGameActivity extends AppCompatActivity{
    private String URL;
    private MyApplication myApplication;
    private ListView switchGame_listView;
    private MyAdapter<SwitchGame> switchGameMyAdapter = null;
    private List<SwitchGame> switchGameList = null;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        switchGame_listView = findViewById(R.id.switchGame_listView);
        OkHttpUtils.get()
                .url(URL + "/switchGame/getSwitchGameList")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(SwitchGameActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        Log.i("success", "获取成功！");
                        switchGameList = new ArrayList<SwitchGame>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                SwitchGame switchGame = new SwitchGame();
                                switchGame.setId(i+1);
                                switchGame.setName(jsonObject.getString("name"));
                                switchGame.setIntroduction(jsonObject.getString("introduction"));
                                switchGame.setLanguage(jsonObject.getString("yuyan"));
                                switchGame.setType(jsonObject.getString("type"));
                                switchGame.setScore(jsonObject.getDouble("score"));
                                switchGame.setImageUrl(jsonObject.getString("imageUrl"));
                                switchGameList.add(switchGame);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        switchGameMyAdapter = new MyAdapter<SwitchGame>((ArrayList) switchGameList, R.layout.switch_list_item) {
                            @Override
                            public void bindView(ViewHolder holder, SwitchGame obj) {
                                imageView = holder.getView(R.id.switchGame_image);
                                Glide.with(SwitchGameActivity.this)
                                        .load(obj.getImageUrl())
                                        .error(R.drawable.error)
                                        .crossFade()
                                        .into(imageView);
                                holder.setText(R.id.id_textView, obj.getId() + "");
                                holder.setText(R.id.name_textView, obj.getName());
                                holder.setText(R.id.type_textView, obj.getType() + " " + obj.getLanguage());
                                holder.setText(R.id.score_textView, "" + obj.getScore());
                            }
                        };
                        switchGame_listView.setAdapter(switchGameMyAdapter);
                        switchGame_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                SwitchGame switchGame = switchGameList.get(position);
                                Intent intent = new Intent(SwitchGameActivity.this, SwitchGameIntroductionActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("name", switchGame.getName());
                                bundle.putDouble("score",switchGame.getScore());
                                bundle.putString("type",switchGame.getType());
                                bundle.putString("yuyan",switchGame.getLanguage());
                                bundle.putString("introduction", switchGame.getIntroduction());
                                bundle.putString("imageUrl", switchGame.getImageUrl());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }
                });
    }
}

